
#ifndef __DEVICE_CONTROLLER_H_
#define __DEVICE_CONTROLLER_H_

/* Includes ------------------------------------------------------------------*/
#include "dlt645_port.h"
#include "mlink_subdevice.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
/* Public typedef ------------------------------------------------------------*/
enum device_mode{
	DEVICE_MODE_AIR_SWITCH_HPERFOR=0, // HPERFOR厂商空开
	DEVICE_MODE_AIR_SWITCH_IMEGA,     // IMEGA厂商空开
	DEVICE_MODE_AIR_SWITCH_HEY,       // HEY厂商空开
	DEVICE_MODE_DTU,
};

//按键屏蔽位定义
typedef enum{
	DEVICE_KEY_DIS_BIT_KEY0 	  = 0,
	DEVICE_KEY_DIS_BIT_NETKEY	  = 6//配网按键固定为6
}device_key_dis_bit_t;

/**
 * @brief The value of the cid corresponding to each attribute of the light
 */
// enum device_cid {

// 	DEVICE_CID_ONOFF 			 = 0,
		
// 	DEVICE_CID_TID   			 = 19,
//     DEVICE_CID_MODE              = 20,
//     DEVICE_CID_TEMPERATURE       = 32,
// 	DEVICE_CID_REPORT_PERIOD     = 49,
// 	DEVICE_CID_REBOOT_TIME       = 45,

// 	DEVICE_CID_POWER			 = 38,
// 	DEVICE_CID_ENERGY        	 = 39, // energy_consumed
// 	DEVICE_CID_VOLTAGE			 = 40,
// 	DEVICE_CID_CURRENT			 = 41,
// 	DEVICE_CID_PF   			 = 42, // power factor

// 	DEVICE_CID_IR_RX_ORIGIN			 = 21,
// 	DEVICE_CID_IR_TX_ORIGIN 		 = 22,
// 	DEVICE_CID_IR_RX_YKY 	    	 = 23,
// 	DEVICE_CID_IR_TX_YKY    		 = 24,
// 	DEVICE_CID_IR_YKY_KEY			 = 110,
// 	DEVICE_CID_IR_YKY_ACTIVE_CODE	 = 111,

// 	DEVICE_CID_DT_CH0_ENABLE      = 64,
// 	DEVICE_CID_DT_CH0_BAUD_RATE   = 65,
// 	DEVICE_CID_DT_CH0_DATA_BITS   = 66,
// 	DEVICE_CID_DT_CH0_STOP_BITS   = 67,
// 	DEVICE_CID_DT_CH0_PARITY      = 68,
// 	DEVICE_CID_DT_CH1_ENABLE      = 69,
// 	DEVICE_CID_DT_CH1_BAUD_RATE   = 70,
// 	DEVICE_CID_DT_CH1_DATA_BITS   = 71,
// 	DEVICE_CID_DT_CH1_STOP_BITS   = 72,
// 	DEVICE_CID_DT_CH1_PARITY      = 73,
// 	DEVICE_CID_DT_CH2_ENABLE	  = 74,
// 	DEVICE_CID_DT_CH2_BAUD_RATE   = 75,
// 	DEVICE_CID_DT_CH2_DATA_BITS   = 76,
// 	DEVICE_CID_DT_CH2_STOP_BITS   = 77,
// 	DEVICE_CID_DT_CH2_PARITY	  = 78,
// 	DEVICE_CID_DT_CH1_485         =103,
// 	DEVICE_CID_DT_CH2_485		  =106,

//     DEVICE_CID_DLT645_ADDR            = 120,
//     DEVICE_CID_DLT645_PHASE           = 121,
//     DEVICE_CID_DLT645_ADDR_WRITE      = 122,
//     DEVICE_CID_DLT645_BAUDRATE_WRITE  = 123,
// 	DEVICE_CID_DLT645_STATUS_A		  = 124,
// 	DEVICE_CID_DLT645_STATUS_B		  = 125,
// 	DEVICE_CID_DLT645_STATUS_C		  = 126,
// 	DEVICE_CID_DLT645_STATUS_ALL	  = 127,

//     DEVICE_CID_DT_IP        = 130,
// 	DEVICE_CID_DT_PORT		= 131,
// 	DEVICE_CID_DT_UDP		= 132,
// 	DEVICE_CID_DT_TCP		= 133,
	
// 	DEVICE_CID_WIFI_DISABLE    =105,
// 	DEVICE_CID_WIFI_SSID_HIDE  =170,

// 	DEVICE_CID_SLAVE_POWER			 = 172,
// 	DEVICE_CID_SLAVE_ENERGY        	 = 173, // energy_consumed
// 	DEVICE_CID_SLAVE_VOLTAGE	     = 174,
// 	DEVICE_CID_SLAVE_CURRENT		 = 175,
// 	DEVICE_CID_SLAVE_PF   			 = 176, // power factor

// 	DEVICE_CID_MASTER_ADDR           = 177,
// };
#define DEVICE_CID_MAX_NUM (20)

#define DEVICE_CID_TID "19"
#define DEVICE_CID_MODE "20"
#define DEVICE_CID_TEMPERATURE "temperature"
#define DEVICE_CID_REPORT_PERIOD "49"
#define DEVICE_CID_REBOOT_TIME "45"
#define DEVICE_CID_POWER_SOURCE "power"

#define DEVICE_CID_DO01 "do01"
#define DEVICE_CID_DO02 "do02"
#define DEVICE_CID_DO03 "do03"
#define DEVICE_CID_DO04 "do04"

#define DEVICE_CID_DI01 "di01"
#define DEVICE_CID_DI02 "di02"
#define DEVICE_CID_DI03 "di03"
#define DEVICE_CID_DI04 "di04"

#define DEVICE_CID_AI01 "ai01"
#define DEVICE_CID_AI02 "ai02"
#define DEVICE_CID_AI03 "ai03"
#define DEVICE_CID_BAT_VOLTAGE "bat_voltage"
#define DEVICE_CID_BAT_LEVEL "bat_level"
// #define DEVICE_CID_AI04 "ai04"

/* Public define -------------------------------------------------------------*/
#define DEVICE_TID_DTU					 (133)
#define DEVICE_GROUP_ID                (14)
#define DEVICE_NAME 				      "PDU"

/* Public macro --------------------------------------------------------------*/

/* Public variables ----------------------------------------------------------*/
extern uint16_t DEVICE_TID;

/* Public function prototypes ------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

esp_err_t device_subdev_0_init(void);
void subdev_io_scan_task(void *arg);
/**
 * @brief 子设备cid数据上报
 * */
esp_err_t subdev_cid_value_report(subdev_port_t *subdev, subdev_cid_t *cid);

int device_report_status(char *cids[], int cids_num);

void device_set_reboot_time(int time);
int device_get_reboot_time();
void device_set_report_time(int time);
int device_get_report_time();
void device_set_mode(int mode);
int device_get_mode();
void device_set_mesh_addr(uint8_t * addr);
void device_get_mesh_addr(uint8_t * addr);
void device_set_default_netif_name(char *netifkey);
char *device_get_default_netif_name(char *netifkey);
void device_set_sn(char *sn);
char *device_get_sn(char *sn);
/*
	设置wifi 关闭/开启
*/
void device_set_wifi_ap_disable(bool value);
bool device_get_wifi_ap_disable();
/*
	设置wifi ssid隐藏功能
*/
void device_set_wifi_ap_ssid_hide(bool value);
bool device_get_wifi_ap_ssid_hide();
void device_set_wifi_ap_ssid_password(const char *ssid, const char *password);
const char *device_get_wifi_ap_ssid(void);
const char *device_get_wifi_ap_password(void);
/*
	设置wifi信道
*/
void device_set_wifi_ap_channel(uint8_t value);
uint8_t device_get_wifi_ap_channel();
void app_set_static_ip_enable(bool enable);
bool app_get_static_ip_enable();
void app_set_static_ip_info(const char *ip, const char *netmask, const char *gw);
int app_get_static_ip_info(char *ip, char *netmask, char *gw);
const char *app_get_username();
int app_set_user_password(char *passwd);
const char *app_get_user_password();

void device_set_temperature(float t);
float device_get_temperature();

int device_set_dt_target_ip(const char *value);
const char * device_get_dt_target_ip();
int device_set_dt_target_port(int value);
int device_get_dt_target_port();

void device_dlt645_set_password(const char *pass);
char *device_dlt645_get_password(void);
esp_err_t device_dlt645_write_data(dlt645_t *p_dlt645, char *addr, char *pass_str, uint32_t ruler_id, float wirite_data);
esp_err_t device_dlt645_get_data(dlt645_t *p_dlt645, char *addr, uint32_t ruler_id, float *out_data);
esp_err_t device_dlt645_d97_write_data(dlt645_t *p_dlt645, char *addr, char *pass_str, uint32_t ruler_id, float wirite_data);
esp_err_t device_dlt645_d97_read_float(dlt645_t *p_dlt645, char *addr, uint32_t ruler_id, float *out_data);
/*
	设置dlt645设备通信速率
*/
esp_err_t device_dlt645_write_baudrate(dlt645_t *p_dlt645, char *addr, int value);
/*
	dlt645清空电量命令
*/
esp_err_t device_dlt645_energy_reset(dlt645_t *p_dlt645, char *pass_str, char *addr);
/*
	设置dlt645子设备地址
*/
esp_err_t device_dlt645_write_addr(dlt645_t *p_dlt645, char *addr);
/*
	读dlt645设备地址 一对一连接的情况下可用
*/
esp_err_t device_dlt645_read_addr(dlt645_t *p_dlt645, char *addr);

/***************************  空开 操作函数       *********************************/
esp_err_t device_dlt645_set_onoff(dlt645_t *p_dlt645, char *addr, char *pass_str, int onoff);
esp_err_t device_dlt645_get_onoff(dlt645_t *p_dlt645, char *addr, int *onoff);
esp_err_t device_dlt645_hperfor_set_onoff(dlt645_t *p_dlt645, char *addr, char *pass_str, int onoff);
/* 空开数据轮询 */
void subdev_dlt645_scan_task(void *arg);

int device_get_power_source(void);

/************************************* IO ********************************************/
enum{
	DEVICE_IO_DO01 = 0,
	DEVICE_IO_DO02,
	DEVICE_IO_DO03,
	DEVICE_IO_DO04,
	DEVICE_IO_DO_MAX,
};
enum{
	DEVICE_IO_DI01 = 0,
	DEVICE_IO_DI02,
	DEVICE_IO_DI03,
	DEVICE_IO_DI04,
	DEVICE_IO_DI_MAX,
};
enum{
	DEVICE_IO_AI01 = 0,
	DEVICE_IO_AI02,
	DEVICE_IO_AI03,
	DEVICE_IO_AI_PT100,
	DEVICE_IO_AI_BATTERY,
	DEVICE_IO_AI_BATTERY_LEVEL,
	DEVICE_IO_AI_MAX,
};
int device_set_do_status(uint8_t index, int status);
int device_get_do_status(uint8_t index);
void device_set_di_status(uint8_t index, int status);
int device_get_di_status(uint8_t index);
esp_err_t device_get_ai_status(uint8_t index, float *out_val);

void device_load_record(void);
esp_err_t device_controller_init(void);
#ifdef __cplusplus
}
#endif
#endif /* __DEVICE_CONTROLLER_H_

*/

